<?php

namespace app\store\controller\order;

use app\store\controller\Controller;
use app\store\model\Order as OrderModel;
use app\store\model\OrderRefund as OrderRefundModel;
use app\store\model\ReturnAddress as ReturnAddressModel;
use app\common\service\order\Refund as RefundService;
use app\api\model\OrderGoods as OrderGoodsModel;
use app\api\model\OrderRefund as OrderRefundModel1;
/**
 * 售后管理
 * Class Refund
 * @package app\store\controller\order
 */
class Refund extends Controller
{
    /**
     * 帮助中心列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new OrderRefundModel;
        $list = $model->getList($this->getData());
        // dump($list);die;
        return $this->fetch('index', compact('list'));
    }

    /**
     * 售后单详情
     * @param $order_refund_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_refund_id)
    {
        // 售后单详情
        $detail = OrderRefundModel::detail($order_refund_id);
        // 订单详情
        $order = OrderModel::detail($detail['order_id']);
        // 退货地址
        $address = (new ReturnAddressModel)->getAll();
        return $this->fetch('detail', compact('detail', 'order', 'address'));
    }

    /**
     * 商家审核
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function audit($order_refund_id)
    {
        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->audit($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
    public function receipt($order_refund_id)
    {
        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->receipt($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
    /**
     * 确认收货并退款
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    // public function receipt($order_refund_id)
    // {
    //     if (!$this->request->isAjax()) {
    //         return false;
    //     }
    //     $order = DB("order")->where("order_id",$order_refund_id)->find();
    //     // 执行原路退款
    //     $data = (new RefundService)->execute($order, $order['pay_price']);
    //     // echo 1;
    //     // dump($data);die;
    //     if (DB("order")->where("order_id",$order_refund_id)->update(['pay_status'=>30])) {
    //         return $this->renderSuccess('操作成功');
    //     }
    //     // if ($model->receipt($this->postData('refund'))) {
            
    //     // }
    //     // return $this->renderError($model->getError() ?: '操作失败');
    // }
    // public function receipt($order_goods_id)
    // {
    //     // echo 1;die;
    //     // dump($order_goods_id);die;
    //     // if (!$this->request->isAjax()) {
    //     //     return false;
    //     // }
    //     // $model = OrderRefundModel::detail($order_refund_id);
    //     // if ($model->receipt($this->postData('refund'))) {
    //     //     return $this->renderSuccess('操作成功');
    //     // }
    //     // return $this->renderError($model->getError() ?: '操作失败');
    //     // dump($order_goods_id);die;
    //     // 订单商品详情
    //     $goods = OrderGoodsModel::detail($order_goods_id);
    //     // dump($goods->user_id);die;
    //     if (isset($goods['refund']) && !empty($goods['refund'])) {
    //         return $this->renderError('当前订单已申请售后');
    //     }
    //     if (!$this->request->isPost()) {
    //         // return $this->renderSuccess(['detail' => $goods]);
    //         return $this->renderSuccess('添加成功', url('/order.refund/index'));
    //         // /store/order.refund/index
    //     }
    //     // 新增售后单记录
    //     $model = new OrderRefundModel;
    //     // dump($model);die;
    //     if ($model->apply($goods->user_id, $goods, $this->request->post())) {
    //         return $this->renderSuccess([], '提交成功');
    //     }
    //     return $this->renderError($model->getError() ?: '提交失败');
    // }
    /**
     * 申请售后
     * @param $order_goods_id
     * @return array
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function apply($order_goods_id)
    {
        // dump($order_goods_id);die;
        // 订单商品详情
        $goods = OrderGoodsModel::detail($order_goods_id);
        if (isset($goods['refund']) && !empty($goods['refund'])) {
            return $this->renderError('当前订单已申请售后');
        }
        // 新增售后单记录
        $model = new OrderRefundModel1;
        
        $user['user_id'] = $goods->user_id;
        $data['type'] = 10;;
        $data['order_goods_id'] = $order_goods_id;
        $data['content'] = "后台管理员操作售后单";
        if ($model->apply($user, $goods, $data)) {
            return $this->renderSuccess('操作成功', url('/store/order.refund/index'));
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

}